/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ep.persistence.controllers;

import ep.persistence.controllers.exceptions.NonexistentEntityException;
import ep.persistence.entities.T012Diagnosis;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ep.persistence.entities.T006Prescription;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author Spyros
 */
public class T012DiagnosisJpaController {

    public T012DiagnosisJpaController() {
        emf = Persistence.createEntityManagerFactory("EprescPersistencePU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(T012Diagnosis t012Diagnosis) {
        if (t012Diagnosis.getT006PrescriptionCollection() == null) {
            t012Diagnosis.setT006PrescriptionCollection(new ArrayList<T006Prescription>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<T006Prescription> attachedT006PrescriptionCollection = new ArrayList<T006Prescription>();
            for (T006Prescription t006PrescriptionCollectionT006PrescriptionToAttach : t012Diagnosis.getT006PrescriptionCollection()) {
                t006PrescriptionCollectionT006PrescriptionToAttach = em.getReference(t006PrescriptionCollectionT006PrescriptionToAttach.getClass(), t006PrescriptionCollectionT006PrescriptionToAttach.getId());
                attachedT006PrescriptionCollection.add(t006PrescriptionCollectionT006PrescriptionToAttach);
            }
            t012Diagnosis.setT006PrescriptionCollection(attachedT006PrescriptionCollection);
            em.persist(t012Diagnosis);
            for (T006Prescription t006PrescriptionCollectionT006Prescription : t012Diagnosis.getT006PrescriptionCollection()) {
                T012Diagnosis oldT012DiagnosisOfT006PrescriptionCollectionT006Prescription = t006PrescriptionCollectionT006Prescription.getT012Diagnosis();
                t006PrescriptionCollectionT006Prescription.setT012Diagnosis(t012Diagnosis);
                t006PrescriptionCollectionT006Prescription = em.merge(t006PrescriptionCollectionT006Prescription);
                if (oldT012DiagnosisOfT006PrescriptionCollectionT006Prescription != null) {
                    oldT012DiagnosisOfT006PrescriptionCollectionT006Prescription.getT006PrescriptionCollection().remove(t006PrescriptionCollectionT006Prescription);
                    oldT012DiagnosisOfT006PrescriptionCollectionT006Prescription = em.merge(oldT012DiagnosisOfT006PrescriptionCollectionT006Prescription);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(T012Diagnosis t012Diagnosis) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T012Diagnosis persistentT012Diagnosis = em.find(T012Diagnosis.class, t012Diagnosis.getId());
            Collection<T006Prescription> t006PrescriptionCollectionOld = persistentT012Diagnosis.getT006PrescriptionCollection();
            Collection<T006Prescription> t006PrescriptionCollectionNew = t012Diagnosis.getT006PrescriptionCollection();
            Collection<T006Prescription> attachedT006PrescriptionCollectionNew = new ArrayList<T006Prescription>();
            for (T006Prescription t006PrescriptionCollectionNewT006PrescriptionToAttach : t006PrescriptionCollectionNew) {
                t006PrescriptionCollectionNewT006PrescriptionToAttach = em.getReference(t006PrescriptionCollectionNewT006PrescriptionToAttach.getClass(), t006PrescriptionCollectionNewT006PrescriptionToAttach.getId());
                attachedT006PrescriptionCollectionNew.add(t006PrescriptionCollectionNewT006PrescriptionToAttach);
            }
            t006PrescriptionCollectionNew = attachedT006PrescriptionCollectionNew;
            t012Diagnosis.setT006PrescriptionCollection(t006PrescriptionCollectionNew);
            t012Diagnosis = em.merge(t012Diagnosis);
            for (T006Prescription t006PrescriptionCollectionOldT006Prescription : t006PrescriptionCollectionOld) {
                if (!t006PrescriptionCollectionNew.contains(t006PrescriptionCollectionOldT006Prescription)) {
                    t006PrescriptionCollectionOldT006Prescription.setT012Diagnosis(null);
                    t006PrescriptionCollectionOldT006Prescription = em.merge(t006PrescriptionCollectionOldT006Prescription);
                }
            }
            for (T006Prescription t006PrescriptionCollectionNewT006Prescription : t006PrescriptionCollectionNew) {
                if (!t006PrescriptionCollectionOld.contains(t006PrescriptionCollectionNewT006Prescription)) {
                    T012Diagnosis oldT012DiagnosisOfT006PrescriptionCollectionNewT006Prescription = t006PrescriptionCollectionNewT006Prescription.getT012Diagnosis();
                    t006PrescriptionCollectionNewT006Prescription.setT012Diagnosis(t012Diagnosis);
                    t006PrescriptionCollectionNewT006Prescription = em.merge(t006PrescriptionCollectionNewT006Prescription);
                    if (oldT012DiagnosisOfT006PrescriptionCollectionNewT006Prescription != null && !oldT012DiagnosisOfT006PrescriptionCollectionNewT006Prescription.equals(t012Diagnosis)) {
                        oldT012DiagnosisOfT006PrescriptionCollectionNewT006Prescription.getT006PrescriptionCollection().remove(t006PrescriptionCollectionNewT006Prescription);
                        oldT012DiagnosisOfT006PrescriptionCollectionNewT006Prescription = em.merge(oldT012DiagnosisOfT006PrescriptionCollectionNewT006Prescription);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = t012Diagnosis.getId();
                if (findT012Diagnosis(id) == null) {
                    throw new NonexistentEntityException("The t012Diagnosis with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T012Diagnosis t012Diagnosis;
            try {
                t012Diagnosis = em.getReference(T012Diagnosis.class, id);
                t012Diagnosis.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The t012Diagnosis with id " + id + " no longer exists.", enfe);
            }
            Collection<T006Prescription> t006PrescriptionCollection = t012Diagnosis.getT006PrescriptionCollection();
            for (T006Prescription t006PrescriptionCollectionT006Prescription : t006PrescriptionCollection) {
                t006PrescriptionCollectionT006Prescription.setT012Diagnosis(null);
                t006PrescriptionCollectionT006Prescription = em.merge(t006PrescriptionCollectionT006Prescription);
            }
            em.remove(t012Diagnosis);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<T012Diagnosis> findT012DiagnosisEntities() {
        return findT012DiagnosisEntities(true, -1, -1);
    }

    public List<T012Diagnosis> findT012DiagnosisEntities(int maxResults, int firstResult) {
        return findT012DiagnosisEntities(false, maxResults, firstResult);
    }

    private List<T012Diagnosis> findT012DiagnosisEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(T012Diagnosis.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public T012Diagnosis findT012Diagnosis(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(T012Diagnosis.class, id);
        } finally {
            em.close();
        }
    }

    public int getT012DiagnosisCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<T012Diagnosis> rt = cq.from(T012Diagnosis.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
